package cn.com.huzo.service.master.impl;

import cn.com.huzo.model.entities.master.SupplierGroupEntity;
import cn.com.huzo.repository.master.SupplierGroupRepository;
import huzofw.service.impl.GenericManagerImpl;
import cn.com.huzo.service.master.SupplierGroupManager;
import cn.com.huzo.service.master.SupplierGroupMemberSupplierManager;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.com.huzo.service.master.SupplierGroupMemberUserManager;
import huzofw.model.entities.paginator.Paginator;
import java.util.List;

/**
 * 供应商组管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("SupplierGroupManager")
public class SupplierGroupManagerImpl extends GenericManagerImpl<SupplierGroupEntity, Long>
        implements SupplierGroupManager {

    @Resource(name = "SupplierGroupRepository")
    private SupplierGroupRepository _dao;

    @Resource(name = "SupplierGroupMemberUserManager")
    private SupplierGroupMemberUserManager _manager_lines_user;

    @Resource(name = "SupplierGroupMemberSupplierManager")
    private SupplierGroupMemberSupplierManager _manager_lines_supplier;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchByHeaderId(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        _result.put("GroupEntity", _dao.fetchByKey(MapUtils.getLong(criterion, "GroupId")));
        Map<String, Object> params = new HashMap();
        params.put("GroupId", MapUtils.getLong(criterion, "GroupId"));
        params.put("Limit", MapUtils.getInteger(criterion, "Limit01"));
        params.put("PageIndex", MapUtils.getInteger(criterion, "PageIndex01"));
        _result.put("MemberUserList", _manager_lines_user.fetchForPage(params));
        params.clear();
        params.put("GroupId", MapUtils.getLong(criterion, "GroupId"));
        params.put("Limit", MapUtils.getInteger(criterion, "Limit02"));
        params.put("PageIndex", MapUtils.getInteger(criterion, "PageIndex02"));
        _result.put("MemberSupplerList", _manager_lines_supplier.fetchForPage(params));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        Long offset = (long) (MapUtils.getInteger(criterion, "Limit") * (MapUtils.getInteger(criterion, "PageIndex") - 1));
        long total = _dao.fetchForCount(criterion);

        if (total > 0) {
            if (offset >= total) {
                offset = (total - MapUtils.getInteger(criterion, "Limit")) < 0 ? 0 : (total - MapUtils.getInteger(criterion, "Limit"));
            }
            criterion.put("Offset", offset);
            List<SupplierGroupEntity> itemList = _dao.fetchForPage(criterion);
            Paginator<SupplierGroupEntity> paginator = new Paginator<>(itemList, offset, MapUtils.getInteger(criterion, "Limit"), total);

            _result.put("TotalRecordCount", paginator.getTotal());
            _result.put("TotalPages", paginator.getTotalPages());
            _result.put("PageIndex", paginator.getPageIndex());
            _result.put("DataList", paginator.getItemList());
            _result.put("Success", true);
        } else {
            _result.put("Success", false);
        }

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Long insert(SupplierGroupEntity instance) throws Exception {
        return _dao.insert(instance);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Integer update(SupplierGroupEntity instance) throws Exception {
        return _dao.update(instance);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        Integer rowsAffected = _dao.delete(MapUtils.getLong(criterion, "GroupId"));
        if (rowsAffected > 0) {
            _result.put("Success", true);
            _result.put("LinesPageData", fetchForPage(criterion));
        } else {
            _result.put("Success", false);
        }

        return _result;
    }
}
